<?php
require_once 'phpmoko/lib/common.php';
require_once 'phpmoko/utils/Curl.php';

class Console
{
	private static $sendCurl = true;

	public static function setCurlEnabled($enabled)
	{
		self::$sendCurl = $enabled;
	}

	private static function isCLI()
	{
		static $sapi_name = null;

		if ($sapi_name == null)
			$sapi_name = php_sapi_name();

		return $sapi_name == 'cli';
	}

	private static function consoleErrorHandle($errno, $errstr, $errfile, $errline)
	{
		if (Console::isCLI()) {
			$out = "";
			switch ($errno) {
				case E_USER_ERROR:
					$out = "31"; // Red
					break;
				case E_USER_WARNING:
					$out = "33"; // Yellow
					break;
				case E_USER_NOTICE:
					$out = "34"; // Blue
					break;
				default:
				case 2048:
					$out = "32"; // Green
					break;
			}
			echo "\033[" . $out . "m";
			echo "(" . basename($errfile) . ":" . $errline . ")\t";
			echo "\033[0m";
			echo $errstr;
			echo "\n";
			return;
		}

		$production = file_exists($_SERVER['DOCUMENT_ROOT'] . '/.pmkprod') ? 'Y' : 'N';

		$backtrace_array = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
		// Quitamos el ConsoleErrorHandle
		while (isset($backtrace_array[0]) && ($backtrace_array[0]['function'] == 'consoleErrorHandle'))
			array_shift($backtrace_array);

		if (self::$sendCurl) {
			$curl = new Curl('http://www.phpmoko.com/logerrors/log.php');
			$curl->setParameter('errstr', $errstr, 'POST');
			$curl->setParameter('errfile', $errfile, 'POST');
			$curl->setParameter('errline', $errline, 'POST');
			$hostname = gethostname();
			$curl->setParameter('servername', (isset($hostname) ? $hostname : 'Unknow'), 'POST');
			$curl->setParameter('production', $production, 'POST');
			$curl->setParameter('backtrace', @json_encode($backtrace_array), 'POST');

			$other = '';
			if (isset($_SERVER["HTTP_REFERER"]))
				$other .= 'HTTP REFERER: ' . $_SERVER["HTTP_REFERER"] . "\n";
			if (isset($_SERVER["SERVER_NAME"]))
				$other .= 'SERVER NAME: ' . $_SERVER["SERVER_NAME"] . "\n";
			$curl->setParameter('other', $other, 'POST');

			$type = '';
			switch ($errno) {
				case E_USER_ERROR:
					$type = 'E';
					break;
				case E_USER_WARNING:
					$type = 'W';
					break;
				case E_USER_NOTICE:
					$type = 'N';
					break;
				default:
				case 2048:
					$type = 'I';
					break;
			}

			$curl->setParameter('type', $type, 'POST');
			$curl->exec();
		}

		if ($production == 'Y')
			return;

		static $id = 0;
		$id++;

		require_once 'phpmoko/ui/RootPanel.php';
		require_once 'phpmoko/externals/JQuery.php';

		$root = RootPanel::get();

		JQuery::activate();

		$backstrace_str = var_export($backtrace_array, true);

		require_once 'phpmoko/ui/SourceFormatter.php';
		$backstrace_str = new SourceFormatter("<?php " . $backstrace_str . "?>", 'PHP');
		$backstrace_str = $backstrace_str->toString();
		$backstrace_str = str_replace("&lt;?php", "", $backstrace_str);
		$backstrace_str = str_replace("?&gt;", "", $backstrace_str);

		$message = '<span id=\'pmk-error-' . $id . '\' style=\'cursor:pointer\' onclick=\'$(this).hide(); $("#pmk-error-full-' . $id . '").show("fast")\'><span style=\'color:blue; text-decoration:none\'>+</span> ' . $errstr . '</span>';
		$message .= '<span id=\'pmk-error-full-' . $id . '\' style=\'display: none; cursor:pointer\' onclick=\'$(this).hide(); $("#pmk-error-' . $id . '").show("fast")\'><span style=\'color:blue; text-decoration:none\'>-</span> ' . $errstr . '<br><div style=\'height: 300px; overflow-x:hidden; overflow-y:scroll;\'>' . $backstrace_str . '</div></span>';

		switch ($errno) {
			case E_USER_ERROR:
				$root->addErrorMessage($message);
				break;
			case E_USER_WARNING:
				$root->addWarningMessage($message);
				break;
			case E_USER_NOTICE:
				$root->addDebugMessage($message);
				break;
			default:
			case 2048:
				$root->addInfoMessage($message);
				break;
		}
	}

	static function _consoleErrorHandle($errno, $errstr, $errfile, $errline)
	{
		Console::consoleErrorHandle($errno, $errstr, $errfile, $errline);
	}

	static function init()
	{
		set_error_handler("Console::_consoleErrorHandle");
	}

	static function disableConsole()
	{
		restore_error_handler();
	}

	static function debug($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(E_USER_NOTICE, $message, $callee['file'], $callee['line']);
	}

	static function error($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(E_USER_ERROR, $message, $callee['file'], $callee['line']);
	}

	static function warn($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(E_USER_WARNING, $message, $callee['file'], $callee['line']);
	}

	static function info($message)
	{
		$callee = current(debug_backtrace());
		Console::consoleErrorHandle(2048, $message, $callee['file'], $callee['line']);
	}
}
?>